package Format;

import bean.HotMovie;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.List;

public class JsonFormat {
    private String jsonStr;
    private String keyName;

    public JsonFormat() {

    }

    public JsonFormat(String jsonStr) {
        this.jsonStr = jsonStr;
    }

    public List<HotMovie> formatJsonArray(List<HotMovie> hotMovies) {
        this.keyName = "subjects";
        JSONObject json = JSONObject.fromObject(jsonStr);
        JSONArray movies = json.getJSONArray(keyName);
        for (Object movie : movies) {
            HotMovie hotMovie = new HotMovie();
            String title = ((JSONObject) movie).getString("title");
            String detail_url = ((JSONObject) movie).getString("url");
            String rate = ((JSONObject) movie).getString("rate");
            hotMovie.setTitle(title);
            hotMovie.setDetailUrl(detail_url);
            hotMovie.setRate(rate);
            hotMovies.add(hotMovie);
        }
        return hotMovies;
    }
}
